<?php

/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-1
 * update Date 2013-11-4
 * update Date 2013-11-5
 * <yl:content type="title"/>
 * 配合其他标签一起使用
 */
class YlContentTaglib {

    public function parse($content, $arr = array(), $domain) {
        if (stripos($content, 'yl:content') !== false) {
            $matches = preg_parse($content, 'yl:content');
            foreach ($matches as $match) {
                $type = getParseOneAttr($match, 'type');
                $type = empty($type) ? 'title' : $type;
                $ellipsis = getParseOneAttr($match, 'ellipsis');
                $iscleartags = getParseOneAttr($match, 'isClearTags');
                $stripslashes = getParseOneAttr($match, 'stripslashes');
                $wordnum = getParseOneAttr($match, 'wordNum');
                $formatString = getParseOneAttr($match, 'formatString');
                
                //去除html标签（包括去除script标签以及script标签中的内容）
                $wipeHtml = getParseOneAttr($match, 'wipeHtml');
                
                
                //是否能够替换其中的关键字
                $replaceKeyword = getParseOneAttr($match, 'replaceKeyword');

                $ellipsis = $ellipsis ? $ellipsis : '...';
                
                //现将html实体转换成单一字符
                $typeContent = html_entity_decode($arr[$type],ENT_QUOTES,"UTF-8");
                //去除html标签以及script标签和内容（目前用在工具栏目生成时）
                if('true' == $wipeHtml){
                    $typeContent = preg_replace('/<script(.*?)<\/script>/is', "", $typeContent);
                    $typeContent = strip_tags($typeContent);
                }
                
                if ($iscleartags === 'true') {
                    $typeContent = strip_tags($typeContent);
                }
                if ('true' == $stripslashes) {
                    //对所有字段进行处理
                    $typeContent = stripslashes($typeContent);
                }
                if (isset($wordnum)) {
                    //替换多个连续空格为一个，防止截取时长短不一
                    $typeContent = preg_replace('/\s(?=\s)/','\\1',$typeContent);
                    $res = common('String')->msubstr($typeContent, 0, $wordnum, 'utf-8', true, $ellipsis);
                } else if (strtolower($type) === 'content') {
                    $res = str_replace("@/", $domain . "/", $typeContent);
                } else {
                    $res = $typeContent;
                }
                if (strtolower($type) === 'updatetime' || strtolower($type) === 'inputtime') {
                    $formatString = $formatString ? $formatString : 'Y-m-d';
                    $res = date($formatString, $typeContent);
                }
                if('true' == $replaceKeyword){
                    $res = taglib('YlUrl')->parse($res);
                }
                
                //处理tags
                if(strtolower($type) == 'tags'){
                    $res = '';
                    $target = getParseOneAttr($match, 'target');
                    $searchUrl = getParseOneAttr($match, 'searchUrl');
                    $separator = getParseOneAttr($match, 'separator');
                    $target = empty($target) ? '_blank' : $target;
                    $separator = empty($separator) ? '&nbsp;&nbsp;' : $separator;
                    $aTags = explode(" ", $arr['tags']);
                    foreach($aTags as $tag){
                        $searchLink = $searchUrl.$tag;
                        $res .= '<a href="'.$searchLink.'" target="'.$target.'">'.$tag.'</a>'.$separator;
                    }
                }
                
                $content = str_replace($match, $res, $content);
            }
        }
        return $content;
    }

}
